package com.hrsystem.controller;

import com.hrsystem.common.Res;
import com.hrsystem.entity.User;
import com.hrsystem.entity.UserLogin;
import com.hrsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Res login(@RequestBody @Validated(UserLogin.class)  User user) {
        User loginUser = userService.login(user);
        if (loginUser == null) {
            return Res.create(0, "登录失败", null);
        } else {
            return Res.create(1, "登录成功", loginUser);
        }
    }

    @PostMapping("/register")
    public Res register(@RequestBody User user) {
        User registerUser = userService.register(user);
        System.out.println(user);
        if (registerUser == null) {
            return Res.create(0, "注册失败", null);
        } else {
            return Res.create(1, "注册成功", user);
        }
    }

    @GetMapping("/token")
    public Res token(HttpServletRequest request) {
        User user = userService.getUserByToken(request);
        return Res.create(1, "用户TOKEN", user);
    }
}
